package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.ProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingPlanProjectVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceimpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;


    @Override
    public List<NursingPlanVo> GetAllNursingPlan() {
        return nursingPlanMapper.GetAllNursingPlan();
    }



    @Override
    public PageResponse<NursingPlanVo> getPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlan> nursingPlanVos =  nursingPlanMapper.getPage(name,status);
        PageResponse<NursingPlanVo> planVoPageResponse = PageResponse.of(nursingPlanVos, NursingPlanVo.class);
        return planVoPageResponse;
    }





    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        // 将controller层传递过来的接收前端参数的类型转换成对应的实体类
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 由于状态不能为null AutoFillInterceptor 也没有自动添加所以需要手动设置
        nursingPlan.setStatus(1);
        //调用护理计划mapper层方法进行添加护理计划
        nursingPlanMapper.insertPlan(nursingPlan);
        // 从controller层传递过来的类型获取相关联的护理项目并为每一个护理项目设置对应的护理项目的id
        nursingPlanDto.getProjectPlans().forEach(nursingPlanProject -> {nursingPlanProject.setPlanId(nursingPlan.getId());});
        //调用护理计划service层的批量添加护理项目方法
        nursingProjectPlanService.insertListProject(nursingPlanDto.getProjectPlans());
    }

    @Override
    public NursingPlanVo getByid(Long id) {
        return nursingPlanMapper.getByid(id);
    }

    @Override
    public void updatePlan(NursingPlanDto nursingPlanDto) {
        //将传递过来的类型转换成与数据库连接的实体类
        NursingPlan Plan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //再根据传递过来的接收类的id获取护理计划信息
        NursingPlanVo byid = getByid(nursingPlanDto.getId());
        //将获取到的护理计划信息关联的护理项目的id放到集合中存储
        List<Long> ids = byid.getProjectPlans().stream().map(NursingPlanProjectVo::getId).collect(Collectors.toList());
        //在调用批量删除护理项目的方法根据id进行删除
        nursingProjectPlanService.deleteByids(ids);
        //判断前端传递过来的护理计划关联的护理项目是否为空，如果不为空增强for循环进行遍历并set关联护理计划的id
        if (CollUtil.isNotEmpty(nursingPlanDto.getProjectPlans())){
            nursingPlanDto.getProjectPlans().forEach(nursingPlanProject -> {
                nursingPlanProject.setPlanId(byid.getId());
            });//再调用批量插入的方法插入到数据库中
            nursingProjectPlanService.insertListProject(nursingPlanDto.getProjectPlans());
        }//最后调改修改护理计划的方法
        nursingPlanMapper.updateNursingPlan(Plan);
    }

    @Override
    public void deleteByid(Long id) {
        nursingProjectPlanService.deleteByid(id);
        nursingPlanMapper.deleteByidPlan(id);
    }

    @Override
    public void updatestatus(Long id, Integer status) {
        nursingPlanMapper.updatestatus(id,status);
    }


}
